package com.bytedance.douyinclouddemo.utils;

public class UNicode2Str {

    public static String convertUnicodeEscapeSequences(String input) {
        StringBuilder result = new StringBuilder();
        int i = 0;
        while (i < input.length()) {
            if (input.startsWith("\\u", i)) {
                // 提取Unicode转义序列
                String unicodeSequence = input.substring(i, i + 6);
                try {
                    // 将转义序列转换为对应的字符
                    char unicodeChar = (char) Integer.parseInt(unicodeSequence.substring(2), 16);
                    result.append(unicodeChar);
                    // 移动索引到转义序列之后
                    i += 6;
                } catch (NumberFormatException e) {
                    // 如果转义序列无效，则原样保留
                    result.append(unicodeSequence);
                    i += 2; // 至少跳过`\\u`
                }
            } else {
                // 如果不是转义序列，则原样保留字符
                result.append(input.charAt(i));
                i++;
            }
        }
        return result.toString();
    }
}
